{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2fe4f1d1-a091-43a5-bf42-91c7f44a2e60",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Processing modulation files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████| 24/24 [00:00<00:00, 450.16it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_OOK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_4ASK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_8ASK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_BPSK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_QPSK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_8PSK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_16PSK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_32PSK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_16APSK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_32APSK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_64APSK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_128APSK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_16QAM.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_32QAM.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_64QAM.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_128QAM.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_256QAM.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_AM-SSB-WC.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_AM-SSB-SC.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_AM-DSB-WC.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_AM-DSB-SC.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_FM.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_GMSK.npy (SNR=6) in 0.00 seconds\n",
      "Processed ./selected_data/SNR_6/X_SNR_6_mod_OQPSK.npy (SNR=6) in 0.00 seconds\n",
      "combined_data.shape:(24, 1000, 1024, 2)\n",
      "All files processed and saved in 0.22 seconds. Saved as ./selected_data/SNR_6/stacked_modulation_data_SNR_6_k1000.npy.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import time\n",
    "from tqdm import tqdm\n",
    "import re\n",
    "import os\n",
    "\n",
    "# 定义调制类型的顺序\n",
    "modulation_order = [\n",
    "    'OOK', '4ASK', '8ASK', 'BPSK', 'QPSK', '8PSK', '16PSK', '32PSK',\n",
    "    '16APSK', '32APSK', '64APSK', '128APSK', '16QAM', '32QAM', '64QAM',\n",
    "    '128QAM', '256QAM', 'AM-SSB-WC', 'AM-SSB-SC', 'AM-DSB-WC', 'AM-DSB-SC',\n",
    "    'FM', 'GMSK', 'OQPSK'\n",
    "]\n",
    "\n",
    "# 查找文件夹中的所有文件\n",
    "file_dir = './selected_data/SNR_6/'  # 假设所有文件都在当前文件夹中\n",
    "files = os.listdir(file_dir)\n",
    "\n",
    "# 使用正则表达式查找文件名中的 SNR 值\n",
    "snr_pattern = re.compile(r\"X_SNR_(\\d+)_mod_(.*).npy\")\n",
    "\n",
    "# 创建空列表来存储所有的numpy数组\n",
    "data_list = []\n",
    "snr_value = None\n",
    "\n",
    "# 显示总开始时间\n",
    "start_time = time.time()\n",
    "\n",
    "# 循环读取并堆叠数据，使用 tqdm 显示进度条\n",
    "for mod in tqdm(modulation_order, desc=\"Processing modulation files\"):\n",
    "    # 根据调制类型找到匹配的文件\n",
    "    matching_files = [f for f in files if f\"mod_{mod}\" in f and snr_pattern.search(f)]\n",
    "    \n",
    "    if matching_files:\n",
    "        # 处理第一个匹配的文件（假设每种调制方式只有一个 SNR 对应）\n",
    "        file_name = os.path.join(file_dir, matching_files[0])\n",
    "        \n",
    "        # 提取 SNR 值\n",
    "        match = snr_pattern.search(file_name)\n",
    "        if match:\n",
    "            snr_value = match.group(1)  # 提取 SNR 的数值\n",
    "            mod_type = match.group(2)   # 提取调制类型\n",
    "            \n",
    "            # 记录每个文件处理的开始时间\n",
    "            file_start_time = time.time()\n",
    "            \n",
    "            # 读取npy文件\n",
    "            data = np.load(file_name)\n",
    "            \n",
    "            # 将每个数据存入列表\n",
    "            data_list.append(data)\n",
    "            \n",
    "            # 记录文件处理结束时间并计算耗时\n",
    "            file_end_time = time.time()\n",
    "            print(f\"Processed {file_name} (SNR={snr_value}) in {file_end_time - file_start_time:.2f} seconds\")\n",
    "    else:\n",
    "        print(f\"No file found for modulation {mod}\")\n",
    "\n",
    "# 确保SNR值被正确识别\n",
    "if snr_value is None:\n",
    "    raise ValueError(\"No SNR value found. Please check the file naming format.\")\n",
    "\n",
    "# 将列表堆叠成形状为 (24, 1000, 1024, 2) 的数组\n",
    "combined_data = np.stack(data_list, axis=0)\n",
    "cls, smaples, single, iq = combined_data.shape\n",
    "print(f\"combined_data.shape:{combined_data.shape}\")\n",
    "\n",
    "# 保存到新的npy文件，文件名包含动态的 SNR 值\n",
    "output_file_name = f'stacked_modulation_data_SNR_{snr_value}_k{smaples}.npy'\n",
    "output_file_name = os.path.join(file_dir, output_file_name)\n",
    "np.save(output_file_name, combined_data)\n",
    "\n",
    "# 显示总耗时\n",
    "end_time = time.time()\n",
    "print(f\"All files processed and saved in {end_time - start_time:.2f} seconds. Saved as {output_file_name}.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71045974-749b-4e46-8138-9e723a8d1c2e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
